package com.example.asset.controller;


import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.asset.common.Result;
import com.example.asset.entity.AssetInformation;
import com.example.asset.entity.AssetMaintain;
import com.example.asset.service.AssetInformationService;
import com.example.asset.service.AssetMaintainService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.util.List;

@Tag(name = "资产维修管理")
@RestController
@RequestMapping("/maintain")
public class AssetMaintainController{
    @Autowired
    private AssetMaintainService assetMaintainService;

    @Autowired
    private AssetInformationService assetInformationService;

    /**
     * 用于json格式测试
     *
     * @return
     */
    @Operation(summary = "列表，用于测试json格式的数据")
    @GetMapping
    public List<AssetMaintain> findList(){
        return assetMaintainService.list();
    }

    /**
     * 维修情况-用于翻页
     *
     * @param pageNum
     * @param pageSize
     * @param assetNumber (defaultValue="")
     * @return
     */
    @Operation(summary = "维修情况-用于翻页")
    @GetMapping("/outpage")
    public Result outPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(defaultValue = "") String assetNumber){
        IPage<AssetMaintain> page = new Page<>(pageNum, pageSize);
        QueryWrapper<AssetMaintain> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(assetNumber), "asset_number", assetNumber);
        queryWrapper.orderByAsc("result");
        queryWrapper.orderByDesc("addtime");
        IPage<AssetMaintain> assetInformationIPage = assetMaintainService.page(page, queryWrapper);
        return Result.success(assetInformationIPage);
    }

    /**
     * 提交维修-用于翻页
     *
     * @param pageNum
     * @param pageSize
     * @param assetNumber (defaultValue="")
     * @return
     */
    @Operation(summary = "提交维修-用于翻页")
    @GetMapping("/inpage")
    public Result inPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(defaultValue = "") String assetNumber){
        IPage<AssetInformation> page = new Page<>(pageNum, pageSize);
        QueryWrapper<AssetInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("asset_state", "正常");
        queryWrapper.like("asset_number", assetNumber);
        IPage<AssetInformation> assetInformationIPage = assetInformationService.page(page, queryWrapper);
        return Result.success(assetInformationIPage);
    }

    /**
     * 提交维修-用于添加资产维修信息
     *
     * @param assetMaintain
     * @return
     */
    @Operation(summary = "提交维修-用于添加资产维修信息")
    @PostMapping
    public Result save(@RequestBody AssetMaintain assetMaintain){
        return Result.success(assetMaintainService.saveAssetMaintain(assetMaintain));
    }

    /**
     * 维修情况-用于更新资产维修结果
     *
     * @param assetMaintain
     * @param assetMaintain
     * @return
     */
    @Operation(summary = "维修情况-用于更新资产维修结果")
    @PostMapping("/update")
    public Result updateResult(@RequestBody AssetMaintain assetMaintain){
        return Result.success(assetMaintainService.updateMaintainResult(assetMaintain, assetMaintain.getResult()));
    }

    /**
     * 维修情况-用于删除指定单个资产信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "维修情况-用于删除指定单个资产信息")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.success(assetMaintainService.removeById(id));
    }


    /**
     * 用于批量删除
     *
     * @param ids
     * @return
     */
    @Operation(summary = "维修情况-用于批量删除")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(assetMaintainService.removeBatchByIds(ids));
    }

    /**
     * 维修情况-导出借出信息
     *
     * @param response
     * @throws Exception
     */
    @Operation(summary = "维修情况-导出维修信息")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception{
        // 从数据库查询出所有的数据
        List<AssetMaintain> list = assetMaintainService.list();

        ExcelWriter writer = ExcelUtil.getWriter(true);

        writer.write(list, true);

        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("资产维修", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();

    }

}